package com.yunyao.common.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

/**
 * @author xingjishuai
 * @date 2024-07-30
 */
public class XmlToJsonConverter {
    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
    private static final XmlMapper XML_MAPPER = new XmlMapper();
    /**
     * 将XML字符串转换为JSON字符串
     *
     * @param xml XML字符串
     * @return JSON字符串
     * @throws Exception 如果转换过程中发生错误
     */
    public static String convertToJson(String xml) throws Exception {
        // 首先，将XML字符串转换为JsonNode对象
        JsonNode rootNode = XML_MAPPER.readTree(xml);

        // 然后，将JsonNode对象转换为JSON字符串
        return JSON_MAPPER.writeValueAsString(rootNode);
    }
//
//    public static void main(String[] args) throws Exception {
//       String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
//               + "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:omgdc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:flowable=\"http://flowable.org/bpmn\" targetNamespace=\"http://www.flowable.org/processdef\">\n"
//               + "  <process id=\"process_07om6uae\" name=\"采购合同流程\">\n"
//               + "    <startEvent id=\"Event_13szl1d\" name=\"开始\">\n"
//               + "      <outgoing>Flow_0tt6ij7</outgoing>\n"
//               + "    </startEvent>\n"
//               + "    <userTask id=\"Activity_1l27xgk\" name=\"发起人\">\n"
//               + "      <incoming>Flow_0tt6ij7</incoming>\n"
//               + "      <outgoing>Flow_0dp2mig</outgoing>\n"
//               + "    </userTask>\n"
//               + "    <sequenceFlow id=\"Flow_0tt6ij7\" sourceRef=\"Event_13szl1d\" targetRef=\"Activity_1l27xgk\" />\n"
//               + "    <userTask id=\"Activity_1c5ykd2\" name=\"运营核验\">\n"
//               + "      <incoming>Flow_0nl2k90</incoming>\n"
//               + "      <outgoing>Flow_1k4m2vg</outgoing>\n"
//               + "    </userTask>\n"
//               + "    <userTask id=\"Activity_0kxg9p6\" name=\"复核\">\n"
//               + "      <incoming>Flow_1k4m2vg</incoming>\n"
//               + "      <outgoing>Flow_1qs5krb</outgoing>\n"
//               + "    </userTask>\n"
//               + "    <sequenceFlow id=\"Flow_1k4m2vg\" sourceRef=\"Activity_1c5ykd2\" targetRef=\"Activity_0kxg9p6\" />\n"
//               + "    <endEvent id=\"Event_132r1qd\" name=\"结束\">\n"
//               + "      <incoming>Flow_1qs5krb</incoming>\n"
//               + "    </endEvent>\n"
//               + "    <sequenceFlow id=\"Flow_1qs5krb\" name=\"\" sourceRef=\"Activity_0kxg9p6\" targetRef=\"Event_132r1qd\" />\n"
//               + "    <userTask id=\"Activity_1wvq90z\" name=\"成本审核\">\n"
//               + "      <incoming>Flow_0dp2mig</incoming>\n"
//               + "      <outgoing>Flow_0nl2k90</outgoing>\n"
//               + "    </userTask>\n"
//               + "    <sequenceFlow id=\"Flow_0dp2mig\" sourceRef=\"Activity_1l27xgk\" targetRef=\"Activity_1wvq90z\" />\n"
//               + "    <sequenceFlow id=\"Flow_0nl2k90\" sourceRef=\"Activity_1wvq90z\" targetRef=\"Activity_1c5ykd2\" />\n"
//               + "  </process>\n"
//               + "  <bpmndi:BPMNDiagram id=\"BPMNDiagram_flow\">\n"
//               + "    <bpmndi:BPMNPlane id=\"BPMNPlane_flow\" bpmnElement=\"process_07om6uae\">\n"
//               + "      <bpmndi:BPMNEdge id=\"Flow_0nl2k90_di\" bpmnElement=\"Flow_0nl2k90\">\n"
//               + "        <di:waypoint x=\"-260\" y=\"-380\" />\n"
//               + "        <di:waypoint x=\"-220\" y=\"-380\" />\n"
//               + "      </bpmndi:BPMNEdge>\n"
//               + "      <bpmndi:BPMNEdge id=\"Flow_0dp2mig_di\" bpmnElement=\"Flow_0dp2mig\">\n"
//               + "        <di:waypoint x=\"-400\" y=\"-380\" />\n"
//               + "        <di:waypoint x=\"-360\" y=\"-380\" />\n"
//               + "      </bpmndi:BPMNEdge>\n"
//               + "      <bpmndi:BPMNEdge id=\"Flow_1qs5krb_di\" bpmnElement=\"Flow_1qs5krb\">\n"
//               + "        <di:waypoint x=\"70\" y=\"-380\" />\n"
//               + "        <di:waypoint x=\"162\" y=\"-380\" />\n"
//               + "      </bpmndi:BPMNEdge>\n"
//               + "      <bpmndi:BPMNEdge id=\"Flow_1k4m2vg_di\" bpmnElement=\"Flow_1k4m2vg\">\n"
//               + "        <di:waypoint x=\"-120\" y=\"-380\" />\n"
//               + "        <di:waypoint x=\"-30\" y=\"-380\" />\n"
//               + "      </bpmndi:BPMNEdge>\n"
//               + "      <bpmndi:BPMNEdge id=\"Flow_0tt6ij7_di\" bpmnElement=\"Flow_0tt6ij7\">\n"
//               + "        <di:waypoint x=\"-592\" y=\"-380\" />\n"
//               + "        <di:waypoint x=\"-500\" y=\"-380\" />\n"
//               + "      </bpmndi:BPMNEdge>\n"
//               + "      <bpmndi:BPMNShape id=\"Event_13szl1d_di\" bpmnElement=\"Event_13szl1d\">\n"
//               + "        <omgdc:Bounds x=\"-628\" y=\"-398\" width=\"36\" height=\"36\" />\n"
//               + "        <bpmndi:BPMNLabel>\n"
//               + "          <omgdc:Bounds x=\"-620\" y=\"-355\" width=\"22\" height=\"14\" />\n"
//               + "        </bpmndi:BPMNLabel>\n"
//               + "      </bpmndi:BPMNShape>\n"
//               + "      <bpmndi:BPMNShape id=\"Activity_1l27xgk_di\" bpmnElement=\"Activity_1l27xgk\">\n"
//               + "        <omgdc:Bounds x=\"-500\" y=\"-420\" width=\"100\" height=\"80\" />\n"
//               + "      </bpmndi:BPMNShape>\n"
//               + "      <bpmndi:BPMNShape id=\"Activity_1c5ykd2_di\" bpmnElement=\"Activity_1c5ykd2\">\n"
//               + "        <omgdc:Bounds x=\"-220\" y=\"-420\" width=\"100\" height=\"80\" />\n"
//               + "      </bpmndi:BPMNShape>\n"
//               + "      <bpmndi:BPMNShape id=\"Activity_0kxg9p6_di\" bpmnElement=\"Activity_0kxg9p6\">\n"
//               + "        <omgdc:Bounds x=\"-30\" y=\"-420\" width=\"100\" height=\"80\" />\n"
//               + "      </bpmndi:BPMNShape>\n"
//               + "      <bpmndi:BPMNShape id=\"Event_132r1qd_di\" bpmnElement=\"Event_132r1qd\">\n"
//               + "        <omgdc:Bounds x=\"162\" y=\"-398\" width=\"36\" height=\"36\" />\n"
//               + "        <bpmndi:BPMNLabel>\n"
//               + "          <omgdc:Bounds x=\"169\" y=\"-355\" width=\"22\" height=\"14\" />\n"
//               + "        </bpmndi:BPMNLabel>\n"
//               + "      </bpmndi:BPMNShape>\n"
//               + "      <bpmndi:BPMNShape id=\"Activity_1wvq90z_di\" bpmnElement=\"Activity_1wvq90z\">\n"
//               + "        <omgdc:Bounds x=\"-360\" y=\"-420\" width=\"100\" height=\"80\" />\n"
//               + "      </bpmndi:BPMNShape>\n"
//               + "    </bpmndi:BPMNPlane>\n"
//               + "  </bpmndi:BPMNDiagram>\n"
//               + "</definitions>\n";
//        String json = convertToJson(xml);
//        System.out.println(json);
//    }
}
